DATABASE_FILE = 'importdata.txt'
#IMAGE_LOCATION = 'http://amos.cse.wustl.edu/live3dstatic/3dmodels/scene000000008/first.jpg'
from models import *
from common.constants import *
import urllib2
import common.meshwriter
import calibrate
from google.appengine.ext import db

handle = file(DATABASE_FILE);
contents = handle.read()
handle.close()

from django.utils import simplejson
decoder = simplejson.JSONDecoder()
pyContents = decoder.decode(contents)

def dbCheck():
	scenes = 0
	corrs = 0
	meshes = 0
	for item in pyContents:
		if item['model'] == 'live3d.scene':
			scenes = scenes + 1
		elif item['model'] == 'live3d.correspondence':
			corrs = corrs + 1
		elif item['model'] == 'live3d.polygon':
			meshes = meshes + 1
	print "DATABASE CHECK."
	print "scenes in file...", scenes
	print "corrs in file...", corrs
	print "meshes in file...", meshes
	print "<br/>"

	scenes = len(Scene.all().fetch(1000))
	corrs = 0
	for start in range(0,10000, 1000):
		group = Correspondence.all().filter('index >=', start).filter('index <', start+1000).fetch(1000)
		corrs = corrs + len(group)
		
	meshes = len(Mesh.all().fetch(1000))
	print "scenes in db...", scenes
	print "corrs in db...", corrs
	print "meshes in db...", meshes
	print "<br/>"
	
	gc = GlobalCounter.get_by_key_name('gc')
	print "scenes in GC...", gc.numScenes
	print "meshes in GC...", gc.numMeshes
	print "corrs in GC...", gc.numCorrespondences
	print "<br/>"
	

def dbDump(section, maxchanges=100000):

	gc = GlobalCounter.get_by_key_name('gc')
	if not gc:
		gc = GlobalCounter(key_name='gc')

	maxScenes = gc.maxScenes
	maxCorrespondences = gc.maxCorrespondences
	maxMeshes = gc.maxMeshes
	numScenes = gc.numScenes
	numCorrespondences = gc.numCorrespondences
	numMeshes = gc.numMeshes
	
	changes = 0

	for item in pyContents:
		if changes > maxchanges:
			break
			
		if section=='scene' and item['model'] == 'live3d.scene':
			index = int(item['pk'])
			s = Scene.get_by_key_name('scene' + str(index))
			if not s:
				
				fields = item['fields']
				
				name = fields['name']
				amosId = fields['amosId']
				if amosId:
					amosId = int(amosId)
				width = fields['sizeU']
				height = fields['sizeV']
				url = fields['url']
				cameraLatitude = fields['cameraLatitude']
				cameraLongitude = fields['cameraLongitude']
				
				imageURL = 'http://amos.cse.wustl.edu/live3dstatic/3dmodels/scene%09d/first.jpg' % index
				result = urllib2.urlopen(imageURL)
				contents = result.read()
				firstImage = db.Blob(contents)
				
				s = Scene(key_name='scene' + str(index),
						  index=index, name=name, amosId=amosId,
						  width=width, height=height,url=url,
						  firstImage=firstImage,
						  cameraLatitude=cameraLatitude,
						  cameraLongitude=cameraLongitude,
						  calibrated=False)
				s.put()
				print "added scene", index
				maxScenes = max(maxScenes, index)
				numScenes = numScenes + 1
				changes = changes + 1
			else:
				print "scene", index, "already exists."
				
			
		elif section=='correspondence' and item['model'] == 'live3d.correspondence':
			index = int(item['pk'])
			corr = Correspondence.get_by_key_name('corr' + str(index))
			if not corr:
				fields = item['fields']
				scene = Scene.get_by_key_name('scene' + str(fields['scene']))
				if not scene:
					raise ValueError
				latitude = float(fields['latitude'])
				longitude = float(fields['longitude'])
				altitude = float(fields['altitude'])
				u = int(fields['u'])
				v = int(fields['v'])
				c = Correspondence(key_name='corr' + str(index), scene=scene,
							index = index, latitude=latitude, longitude=longitude,
							altitude=altitude,u=u,v=v)
				c.put()
				print "added corr", index
				maxCorrespondences = max(maxCorrespondences, index)
				numCorrespondences = numCorrespondences + 1
				changes = changes + 1
			else:
				pass
				#print "corr", index, "already exists"
			
		
		elif section=='mesh' and item['model'] == 'live3d.polygon':
			index = int(item['pk'])
			mesh = Mesh.get_by_key_name('mesh' + str(index))
			if not mesh:
				
				fields = item['fields']
				scene = Scene.get_by_key_name('scene' + str(fields['scene']))
				if not scene:
					raise ValueError
				name = fields['name']
				points = int(fields['points'])
				if points == 3:
					triangles = [0, 1, 2]
					correspondenceMapping = [int(fields['point0']), int(fields['point1']), int(fields['point2'])]
				elif points == 4:
					triangles = [0, 1, 2, 0, 2, 3]
					correspondenceMapping = [int(fields['point0']), int(fields['point1']), int(fields['point2']), int(fields['point3'])]
				
				corrs = []
				for i in correspondenceMapping:
					c = Correspondence.get_by_key_name('corr' + str(i))
					if not c:
						raise ValueError
					c.parentMeshes = c.parentMeshes + 1;
					c.put()
					corrs.append(c)
					
				originLat, originLon, originAlt, \
				daePositions, daeNormals, daeUVs, daeTriangles, \
				nPositions, nNormals, nUVs, nTriangles = \
					common.meshwriter.writeMeshFile(corrs, triangles, scene.width, scene.height)
				
				m = Mesh(key_name='mesh' + str(index), scene=scene,
						index = index, name=name, points=points, triangles=triangles,
						correspondenceMapping = correspondenceMapping,
						daePositionArray = daePositions,
						daeNormalArray = daeNormals,
						daeUVArray = daeUVs,
						daeTriangles = daeTriangles,
						nPositions = nPositions,
						nNormals = nNormals,
						nUVs = nUVs,
						nTriangles = nTriangles,
						originLatitude = originLat,
						originLongitude = originLon,
						originAltitude = originAlt)
				m.put()
				print "added mesh", index
				maxMeshes = max(maxMeshes, index)
				numMeshes = numMeshes + 1
				changes = changes + 1
			else:
				print "mesh", index, "already exists"
			
	
	
	if section == 'scene':
		gc.maxScenes = maxScenes
		gc.numScenes = numScenes
	elif section == 'correspondence':
		gc.maxCorrespondences = maxCorrespondences
		gc.numCorrespondences = numCorrespondences
	elif section == 'mesh':
		gc.maxMeshes = maxMeshes
		gc.numMeshes = numMeshes
	gc.put()
	print "updated GlobalCounter."

	print "Done!"
	
def calibrateAll(maxchanges=10, start=0):
	allScenes = Scene.all().filter('index >=', start).order('index').fetch(40)
	changes = 0
	for scene in allScenes:
		if changes >= maxchanges:
			break
		corrs = Correspondence.all().filter('scene =', scene).fetch(CORRESPONDENCES_REQUIRED_FOR_CALIBRATION + 1)
		if len(corrs) >= CORRESPONDENCES_REQUIRED_FOR_CALIBRATION and not scene.calibrated:
			calibrate.calibrateCamera(scene)
			changes = changes + 1
			print "calibrated camera", scene.index
		elif scene.calibrated:
			print "camera", scene.index, "already calibrated."
		else:
			print "camera", scene.index, "not enough correspondences."
			
	if len(allScenes) > 0:
		print "start next calibration at", scene.index
	else:
		print "no more scenes."

#dbCheck()
#dbDump('scene')
#dbDump('correspondence')		
#dbDump('mesh', 5)
#calibrateAll(maxchanges=20, start=272)	